package com.warren.blog.config;

import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@EnableWebSecurity //开启WebSecurity模式
public class SecurityConfig extends WebSecurityConfigurerAdapter { //自定义security策略
    @Override
    protected void configure(HttpSecurity http) throws Exception {
//        super.configure(http);
        http.authorizeRequests().antMatchers("/").permitAll()
                .antMatchers("/router/hello").hasRole("vip1")
                .antMatchers("/router/hello1").hasRole("vip2")
                .antMatchers("/router/hello2").hasRole("vip3");

        //没有权限去默认登陆页面 http.formLogin.loginPage("")
        http.formLogin();
        http.logout().logoutUrl("/");
        //开启记住我
        http.rememberMe();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception { //开启WebSecurity模式
//        super.configure(auth);
        //正常是数据库读取，spring security
        auth.inMemoryAuthentication().withUser("root").password(new BCryptPasswordEncoder(4).encode("123")).roles("vip1", "vip2", "vip3")
        .and().
                withUser("guest").password(new BCryptPasswordEncoder(4).encode("123")).roles("vip1")
                .and().withUser("warren").password(new BCryptPasswordEncoder(4).encode("123")).roles("vip2");

    }
}
